﻿package com.toncen.samepms.portal.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.toncen.samepms.common.AbstractDatabaseAction;
import com.toncen.samepms.common.IDomain;
import com.toncen.samepms.net.dao.ChannelDao;
import com.toncen.samepms.net.dao.InformationDao;
import com.toncen.samepms.net.domain.Channel;
import com.toncen.samepms.net.domain.Information;
import com.toncen.samepms.util.Constant;
import com.opensymphony.xwork2.ActionContext;

/**
 * 描述：信息表NET_INFORMATION的action文件
 * 
 * @author xyp
 * @time 2013-5-10
 */
public class InformationPublicAction extends AbstractDatabaseAction<Information, InformationDao> {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1365475264052L;

	public InformationPublicAction() throws Exception {
		super();
	}

	/**
	 * 当前信息所属栏目
	 */
	private Channel channel;

	/**
	 * 信息分页大小
	 */
	private int pagesize = 5;

	/**
	 * 信息标题显示长度
	 */
	private int titlesize = 20;

	/**
	 * 信息显示集合
	 */
	private List<Information> informations = new ArrayList<Information>();

	/**
	 * 获取指定数目的信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String onDoBrowers() throws Exception {
		channel = new ChannelDao().find(channel);
		if (channel != null) {
			setInformations(getDao().getInformationByChannelId(channel.getId(), pagesize));
		}
		return SUCCESS;
	}

	/**
	 * 获取指定数目的图片
	 * 
	 * @return
	 * @throws Exception
	 */
	public String onDoImages() throws Exception {
		long channelId = IDomain.UNSAVED_ID;
		if (channel != null) {
			channelId = channel.getId();
		}
		setInformations(getDao().getImageByChannelId(channelId, pagesize));
		return SUCCESS;
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String onDoBrower() throws Exception {
		prepareOnDoView();
		ChannelDao channelDao = new ChannelDao();
		channel = channelDao.find(channel);
		if (channel != null) {
			channelDao.loadCascadeParent(channel);
		}
		return SUCCESS;
	}

	/**
	 * 获取总的浏览次数(每调用一次，总的浏览次数增1)
	 * 
	 * @return the sumTime
	 */
	@SuppressWarnings("unchecked")
	public long getSumBrowseCount() throws Exception {
		Information information = getDomain();
		Map<Long, Long> sumBrowseCountMap = (Map) ActionContext.getContext().getApplication().get(information.getClass().getName());
		if (sumBrowseCountMap == null) {
			sumBrowseCountMap = new HashMap<Long, Long>(5);
			ActionContext.getContext().getApplication().put(information.getClass().getName(), sumBrowseCountMap);
		}
		long sumBrowseCount = 1;
		if (sumBrowseCountMap.containsKey(information.getId())) {
			sumBrowseCount += sumBrowseCountMap.get(information.getId());
		} else {
			sumBrowseCount += getDomain().getBrowsecount();
		}
		if (sumBrowseCount % Constant.NEWS_BROWSE_TIME_UPDATE_FREQUENCE == 0) {// 每浏览NEWS_BROWSE_TIME_UPDATE_FREQUENCE次则更新一次数据库
			information.setBrowsecount((int) sumBrowseCount);
			getDao().update(information);
		}
		sumBrowseCountMap.put(information.getId(), sumBrowseCount);
		return sumBrowseCount;
	}

	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public List<Information> getInformations() {
		return informations;
	}

	public void setInformations(List<Information> informations) {
		this.informations = informations;
	}

	public int getTitlesize() {
		return titlesize;
	}

	public void setTitlesize(int informationsize) {
		this.titlesize = informationsize;
	}
}
